package com.lottery.ruok.lottrey.fragment;

import com.applozic.mobicomkit.ApplozicClient;
import com.lottery.ruok.lottrey.eventbusObject.LogOut;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by ASUS on 2017/7/27.
 */

public abstract class BaseMainFragment extends BaseFragment{
    /** Fragment当前状态是否可见 */
    protected boolean isVisible;

    //setUserVisibleHint  adapter中的每个fragment切换的时候都会被调用，如果是切换到当前页，那么isVisibleToUser==true，否则为false
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    protected void logOut(){
        UserInfo.setToken("");
        UserInfo.setAvatar("");
        UserInfo.setRoomId("");
        UserInfo.setAdmin("");
        ApplozicClient.getInstance(getActivity()).setRoomId(0);

        EventBus.getDefault().post(new LogOut());
    }

    /**
     * 可见
     */
    protected void onVisible() {
        lazyLoad();
    }


    /**
     * 不可见
     */
    protected void onInvisible() {


    }

    /**
     * 延迟加载
     * 子类必须重写此方法
     */
    protected abstract void lazyLoad();
}
